﻿/**
 * 在线考试
 * @param  {[type]} $                         
 */
define(['jquery', 'bootbox', 'bootstrap'], function ($, bootbox) {
    var videospan = $('.question-video .videomodel');//视频弹出点击span
    var worksubmit = $('.right-bottom-btn');//提交按钮
    var allquesnum = 9;//总题目数量
    var markArr = [];//题目标记集合
    var checkStepGather = []; //存放每个多选题的答案
    var num = [];//多选题选项标记选择数量集合

    //遍历多选题
    var len = $('.question-check .question-cont').length;
    for (var k = 0; k < len; k++) { num[k] = 0; }

    //#region 事件
    //单选题
    $('.question-radio').delegate('li', 'click', function () {
        var $this = $(this);
        choiceClick($this, 1);
    });
    //多选题
    $('.question-check').delegate('li', 'click', function () {
        var $this = $(this);
        choiceClick($this, 2);
    });
    //填空题
    $('.question-blank').delegate('textarea', 'keyup', function () {
        var $this = $(this).parent();
        choiceClick($this, 3);
    });
    //判断题
    $('.question-judge').delegate('li', 'click', function () {
        var $this = $(this);
        $(this).attr('data-value', 1).siblings().attr('data-value', 0);
        $(this).addClass('cur').siblings().removeClass('cur');
        $(this).find('input').prop('checked', true).parents('li').siblings().find('input').prop('checked', false);
        choiceClick($this, 4);
    });
    //视频题
    $('.question-video').delegate('li', 'click', function () {
        var $this = $(this);
        choiceClick($this, 5);
    });

    //#endregion

    //#region 方法
    //标记
    $('.left-content').delegate('.question-mark', 'click', function () {
        var $this = $(this);
        var name = $this.attr('data-name');
        var areaMark = $('.' + name).find('a');
        var identifier = $this.attr('identifier');
        mark($this, areaMark, identifier);
    });

    //答案和答题卡样式设置
    function collatingAnswers(type, flag, itemIndex, identifier, value, $this) {
        $('[name=' + identifier + ']').removeClass('doing done').addClass('doing');
        switch (type) {
            case 1:
                if (flag == 0) {
                    $this.attr('data-value', 1).siblings().attr('data-value', 0);
                    $this.addClass('cur').siblings().removeClass('cur');
                } else {
                    $this.attr('data-value', 0).siblings().attr('data-value', 0);
                    $this.removeClass('cur');
                    $('.radio-tab a').eq(itemIndex).removeClass('doing done');
                }
                break;
            case 2:
                var lis = $('.question-check ul').eq(itemIndex).find('li');
                if (flag == 0) {
                    checkStepGather = [];
                    $this.attr('data-value', 1);
                    $this.addClass('cur');
                    num[itemIndex] += 1;
                    for (var k = 0; k < lis.length; k++) {
                        if (lis.eq(k).hasClass('cur')) {
                            checkStepGather.push(lis.eq(k).find('i').text());
                        }
                    }
                } else {
                    $this.attr('data-value', 0);
                    $this.removeClass('cur');
                    num[itemIndex] -= 1;
                    if (num[itemIndex] <= 0) {
                        $('.check-tab a').eq(itemIndex).removeClass();
                    };
                    checkStepGather.del(value);
                }
                checkStepGather.sort();
                break;
            case 3:
                if ($.trim($this.find('textarea').val()).length > 0) {
                    $this.attr('data-value', 1);
                } else {
                    $this.attr('data-value', 0);
                    $('.blank-tab a').eq(itemIndex).removeClass('doing done');
                }
                break;
            case 4:
                if ($this.find('input').prop('checked')) {
                    var text = $this.find('i').text();
                }
                break;
            case 5:
                if (flag == 0) {
                    $this.attr('data-value', 1).siblings().attr('data-value', 0);
                    $this.addClass('cur').siblings().removeClass('cur');
                } else {
                    $this.attr('data-value', 0).siblings().attr('data-value', 0);
                    $this.removeClass('cur');
                    $('.video-tab a').eq(itemIndex).removeClass('doing done');
                }
                break;
        }
    }
    //选项点击
    function choiceClick($this, type) {
        //把正在做的题目标记已做完
        if ($('.doing').length > 0) {
            $('.doing').removeClass('doing').addClass('done');
        }
        var flag = $this.attr('data-value');
        var btns = $('.content-style a');
        var itemIndex = $this.parent().prevAll('.question-text').attr('index'); //题目索引
        var identifier = $this.parent().prevAll('.question-text').attr('identifier'); //题目ID
        var value = $this.find('i').text();
        collatingAnswers(type, flag, itemIndex, identifier, value, $this);
    }
    //标记
    function mark($this, areaMark, identifier) {
        var index = $this.attr('index');
        var flag = $this.attr('data-value');
        if (flag == 0) {
            $this.attr('data-value', 1).css({ 'color': '#1a8ac9' }).find('img').attr('src', 'work_image/marked.png');
            areaMark.eq(index).addClass('mark');
            markArr.push(identifier);
        } else {
            $this.attr('data-value', 0).css({ 'color': '#888' }).find('img').attr('src', 'work_image/mark.png');
            areaMark.eq(index).removeClass('mark');
            markArr.del(identifier);
        }
    }
    //视频模式化弹出窗口
    videospan.click(function () {
        var divmess = $('#videopopdiv').html();
        bootbox.dialog({
            message: divmess,
            size: 'large',
            buttons: {
                Cancel: {
                    label: '关闭',
                    className: 'bggreen'
                }
            }
        });
        //弹出的视频题
        $('.question-video-pop').delegate('li', 'click', function () {
            var $this = $(this);
            choiceClick($this, 5);
        });
    });
    //提交按钮模式化弹出窗口
    worksubmit.click(function () {
        var divmess = $('#worksubmitdiv').html();
        bootbox.dialog({
            message: divmess,
            size: 'small',
            buttons: {
                Cancel: {
                    label: '再看一下',
                    className: 'bggreen'
                },
                OK: {
                    label: '确定提交',
                    className: 'bgsubmit',
                    callback: function () {
                        alert('提交成功！');
                    }
                }
            }
        });
    });
    //锚点跳转
    $('.resultshow').delegate('a', 'click', function () {
        var name = $(this).attr('name');
        document.getElementById(name).scrollIntoView();
    });
    //智能定位
    $(document).scroll(function () {
        var nScrollTop = $(this).scrollTop();
        if (nScrollTop > 180) {
            //$('.content-style').css({ 'top': nScrollTop - 180 });
        } else {
            //$('.content-style').css({ 'top': 0 });
        }
    });
    //从数组中删除指定数据
    Array.prototype.indexOf = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };
    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };
    Array.prototype.del = function (value) {
        var len = this.length;
        for (var i = 0, n = 0; i < len; i++) {
            if (this[i] != value) {
                this[n++] = this[i];
            } else {
            }
        }
        this.length = n;
    };
    //#endregion
});